package client

import (
	"gorm.io/gorm"
	"likeadmin/admin/schemas/req"
	"likeadmin/admin/schemas/resp"
	"likeadmin/core/request"
	"likeadmin/core/response"
	"likeadmin/model/client"
	"likeadmin/util"
	"time"
)

type IAppConfigsService interface {
	All() (res []resp.AppConfigsResp, e error)
	List(page request.PageReq, listReq req.AppConfigsListReq) (res response.PageResp, e error)
	Detail(id int) (res resp.AppConfigsResp, e error)
	Add(addReq req.AppConfigsAddReq) (e error)
	Edit(editReq req.AppConfigsEditReq) (e error)
	Del(id int) (e error)
}

// NewAppConfigsService 初始化
func NewAppConfigsService(db *gorm.DB) IAppConfigsService {
	return &appConfigsService{db: db}
}

// appConfigsService 应用配置服务实现类
type appConfigsService struct {
	db *gorm.DB
}

// All
func (srv appConfigsService) All() (res []resp.AppConfigsResp, e error) {
	var depts []client.AppConfigs
	err := srv.db.Model(client.AppConfigs{}).Where("status = ? AND delete_time = ?", 1, 0).Order("id desc").Find(&depts).Error
	if e = response.CheckErr(err, "All Find err"); e != nil {
		return
	}
	res = []resp.AppConfigsResp{}
	response.Copy(&res, depts)
	return
}

// List 应用配置列表
func (srv appConfigsService) List(page request.PageReq, listReq req.AppConfigsListReq) (res response.PageResp, e error) {
	// 分页信息
	limit := page.PageSize
	offset := page.PageSize * (page.PageNo - 1)
	// 查询
	model := srv.db.Model(&client.AppConfigs{})
	if listReq.Title != "" {
		model = model.Where("title like ?", "%"+listReq.Title+"%")
	}
	if listReq.PackageName != "" {
		model = model.Where("package_name like ?", "%"+listReq.PackageName+"%")
	}
	if listReq.AppId != "" {
		model = model.Where("app_id = ?", listReq.AppId)
	}
	if listReq.Status >= 0 {
		model = model.Where("status = ?", listReq.Status)
	}

	model = model.Where("delete_time = ? ", 0)

	// 总数
	var count int64
	err := model.Count(&count).Error
	if e = response.CheckErr(err, "List Count err"); e != nil {
		return
	}
	// 数据
	var objs []client.AppConfigs
	err = model.Limit(limit).Offset(offset).Order("id desc").Find(&objs).Error
	if e = response.CheckErr(err, "List Find err"); e != nil {
		return
	}
	resps := []resp.AppConfigsResp{}

	response.Copy(&resps, objs)

	for i, configsResp := range resps {
		resps[i].PayFlags = util.StringUtil.ExplodePreSubffix(configsResp.PayFlag, "-")
	}

	return response.PageResp{
		PageNo:   page.PageNo,
		PageSize: page.PageSize,
		Count:    count,
		Lists:    resps,
	}, nil
}

// Detail 应用配置详情
func (srv appConfigsService) Detail(id int) (res resp.AppConfigsResp, e error) {
	var obj client.AppConfigs
	err := srv.db.Where("id = ?", id).Limit(1).First(&obj).Error
	if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
		return
	}
	if e = response.CheckErr(err, "Detail First err"); e != nil {
		return
	}
	response.Copy(&res, obj)

	res.PayFlags = util.StringUtil.ExplodePreSubffix(obj.PayFlag, "-")

	return
}

// Add 应用配置新增
func (srv appConfigsService) Add(addReq req.AppConfigsAddReq) (e error) {
	var obj client.AppConfigs
	response.Copy(&obj, addReq)

	obj.PayFlag = util.StringUtil.ImplodePreSubffix(addReq.PayFlags, "-")
	err := srv.db.Create(&obj).Error
	e = response.CheckErr(err, "Add Create err")
	return
}

// Edit 应用配置编辑
func (srv appConfigsService) Edit(editReq req.AppConfigsEditReq) (e error) {
	var obj client.AppConfigs
	err := srv.db.Where("id = ?", editReq.Id).Limit(1).First(&obj).Error
	// 校验
	if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
		return
	}
	if e = response.CheckErr(err, "Edit First err"); e != nil {
		return
	}
	// 更新
	err = srv.db.Model(client.AppConfigs{}).Where("id = ?", editReq.Id).Select("title", "package_name", "app_id", "app_secret", "version", "pay_flag", "status").Updates(client.AppConfigs{
		Title:       editReq.Title,
		PackageName: editReq.PackageName,
		AppId:       editReq.AppId,
		AppSecret:   editReq.AppSecret,
		Version:     editReq.Version,
		PayFlag:     util.StringUtil.ImplodePreSubffix(editReq.PayFlags, "-"),
		Status:      editReq.Status,
	}).Error
	e = response.CheckErr(err, "Edit Updates err")
	return
}

// Del 应用配置删除
func (srv appConfigsService) Del(id int) (e error) {
	var obj client.AppConfigs
	err := srv.db.Where("id = ?", id).Limit(1).First(&obj).Error
	// 校验
	if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
		return
	}
	if e = response.CheckErr(err, "Del First err"); e != nil {
		return
	}
	// 删除
	// 删除
	err = srv.db.Model(client.AppConfigs{}).Where("id = ?", obj.ID).Select("delete_time", "status").Updates(&client.PayConfigs{
		DeleteTime: time.Now().Unix(),
		Status:     0,
	}).Error

	e = response.CheckErr(err, "Del Delete err")
	return
}
